home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d11 / frasrc14.arc / CONFIG.C < prev    next >
Text File  |  1990-08-02  |  12KB  |  395 lines

  1. /*
  2.     config.c - FRACTINT code relative to video configuration
  3. */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #ifdef __TURBOC__
  9. #include <dir.h>
  10. #endif
  11.  
  12. #include "fractint.h"
  13. #include "fractype.h"
  14. #include "targa_lc.h"
  15. int file_type = -1;    /* 0=GIF, 1=Tim's pot (may become Targa) TW 7/20/89 */
  16.  
  17. int filetype;
  18. extern char far *resume_info;    /* pointer to resume info if allocated */
  19. extern int  resume_len;     /* length of resume info */
  20. extern char FormName[40];    /* formula name */
  21.  
  22. find_fractal_info(gif_file,info)
  23. char *gif_file;
  24. struct fractal_info *info;
  25. {
  26.    FILE *fp;
  27.    unsigned char gifstart[18];
  28.    int rows, cols, colors;
  29.    char temp1[81];
  30.    int scan_extend, block_type, block_len;
  31.    char far *resume_load;
  32.  
  33.    strcpy(temp1,gif_file);
  34.    if (strchr(temp1,'.') == NULL) {
  35.       strcat(temp1,DEFAULTFRACTALTYPE);
  36.       if ((fp = fopen(temp1,"rb")) != NULL) {
  37.      fclose(fp);
  38.      }
  39.       else {
  40.      strcpy(temp1,gif_file);
  41.      strcat(temp1,ALTERNATEFRACTALTYPE);
  42.      }
  43.       }
  44.  
  45.    if((fp = fopen(temp1,"rb"))==NULL)
  46.    {
  47.       *gif_file = 0; /* failed ... zap filename */
  48.       return(-1);
  49.    }
  50.    fread(gifstart,18,1,fp);
  51.    if (strncmp(gifstart,"GIF",3)==0)
  52.       filetype = 0; /* GIF */
  53.    else  if (strncmp(gifstart,"TIW16BIT",8)==0)
  54.       filetype = 1; /* TIW file type - change to Targa */
  55.    else if(fread(info,sizeof(struct fractal_info),1,fp)==1 &&
  56.          strncmp(info->info_id,"Fractal",8)==0)
  57.       filetype = 2; /* Targa 16 */
  58.    else
  59.    {
  60.       *gif_file = 0; /* failed ... zap filename */
  61.       fclose(fp);
  62.       return(-1);
  63.    }
  64.    switch(filetype)
  65.    {
  66.    case 0: /* GIF */
  67.       rows = gifstart[7]*256+gifstart[6];
  68.       cols = gifstart[9]*256+gifstart[8];
  69.       colors = 2 << (gifstart[10] & 7);
  70.       break;
  71.    case 1: /* TIW */
  72.       cols   = *((int *)(gifstart+ 8));
  73.       rows   = *((int *)(gifstart+10));
  74.       colors = *((int *)(gifstart+12));
  75.       break;
  76.    case 2: /* TARGA */
  77.       rows = *(int *)&gifstart[O_VSIZE];
  78.       cols = *(int *)&gifstart[O_HSIZE];
  79.       colors = info->colors;
  80.       if(rows != info->ydots || cols != info->xdots)
  81.       {
  82.      *gif_file = 0; /* failed ... zap filename */
  83.      fclose(fp);
  84.      return(-1);
  85.       }else
  86.       {
  87.      fclose(fp);
  88.      return(0);
  89.       }
  90.       break;
  91.    default:
  92.       *gif_file = 0; /* failed ... zap filename */
  93.       fclose(fp);
  94.       return(-1);
  95.       break;
  96.    }
  97.  
  98.    if (resume_info != NULL) /* free the prior area if there is one */
  99.    {
  100.       farmemfree(resume_info);
  101.       resume_info = NULL;
  102.    }
  103.  
  104.    /* Format of .fra (*not* part of the GIF standard) extension blocks is:
  105.       1 byte    '!', extension block identifier
  106.       1 byte    extension block number, arbitary? we always plug 255
  107.       1 byte    length of id, 11 with fractint
  108.       n bytes   alpha id, "fractintnnn" with fractint, nnn is secondary id
  109.       1 word    length of block info in bytes
  110.             (note that before fractint vsn 14, this was just a byte)
  111.       x bytes   block info
  112.       1 byte    0, block terminator
  113.       1 byte    ';', GIF terminator
  114.       To scan extension blocks, we first look in file at length of save_info
  115.       (the main extension block) from end of file, looking for a literal known
  116.       to be at start of our block info.  Then we scan forward a bit, in case
  117.       the file is from an earlier fractint vsn with shorter save_info.
  118.       If save_info is found and is from vsn>=14, it includes the total length
  119.       of all extension blocks; we then scan them all first to last to load
  120.       any optional ones which are present.
  121.       Defined extension blocks:
  122.     fractint001    header, always present
  123.     fractint002    resume info for interrupted resumable image
  124.     fractint003    additional formula type info
  125.    */
  126.  
  127.    fseek(fp,(long)(-2-sizeof(FRACTAL_INFO)),SEEK_END);
  128.    fread(info,1,sizeof(FRACTAL_INFO),fp);
  129.    if (strcmp(INFO_ID,info->info_id))
  130.    {  /* didn't work 1st try, maybe an older vsn, maybe junk at eof, scan: */
  131.       int offset,i;
  132.       char tmpbuf[110];
  133.       offset = 80; /* don't even check last 80 bytes of file for id */
  134.       while (offset < sizeof(FRACTAL_INFO)+512) /* allow 512 garbage at eof */
  135.       {
  136.      offset += 100; /* go back 100 bytes at a time */
  137.      fseek(fp,(long)(0-offset),SEEK_END);
  138.      fread(tmpbuf,1,110,fp); /* read 10 extra for string compare */
  139.      for (i = 0; i < 100; ++i)
  140.         if (!strcmp(INFO_ID,&tmpbuf[i])) /* found header? */
  141.         {
  142.            fseek(fp,(long)(i-offset),SEEK_END);
  143.            fread(info,1,sizeof(FRACTAL_INFO),fp);
  144.            offset = 10000; /* force exit from outer loop */
  145.            break;
  146.         }
  147.       }
  148.    }
  149.  
  150.    if (!strcmp(INFO_ID,info->info_id)) /* we found and read info */
  151.    {
  152.       if (info->version >= 4) /* load any additional extension blocks */
  153.       {
  154.      fseek(fp,1L-info->tot_extend_len,SEEK_CUR);
  155.      scan_extend = 1;
  156.      while (scan_extend)
  157.      {
  158.         if (fgetc(fp) != '!' /* if not what we expect just give up */
  159.           || fread(temp1,1,13,fp) != 13
  160.           || strncmp(&temp1[2],"fractint",8))
  161.            break;
  162.         temp1[13] = 0;
  163.         block_type = atoi(&temp1[10]); /* e.g. "fractint002" */
  164.         block_len = (unsigned char)fgetc(fp);
  165.         switch (block_type)
  166.         {
  167.            case 1: /* back to "fractint001", we're all done */
  168.           scan_extend = 0;
  169.           break;
  170.            case 2: /* resume info */
  171.           if ((resume_info = farmemalloc((long)block_len)) == NULL)
  172.           {
  173.              info->calc_status = 3; /* not resumable after all */
  174.              fseek(fp,(long)block_len,SEEK_CUR);
  175.           }
  176.           else
  177.           {
  178.              resume_load = resume_info;
  179.              resume_len = block_len;
  180.              while (--block_len >= 0) /* gross but it works */
  181.             *(resume_load++) = fgetc(fp);
  182.           }
  183.           break;
  184.            case 3: /* formula info */
  185.           fread(FormName,40,1,fp);
  186.           /* perhaps in future add more here, check block_len for
  187.              backward compatibility */
  188.           break;
  189.            default:
  190.           fseek(fp,(long)block_len,SEEK_CUR); /* skip unrecognized block */
  191.         }
  192.         if (fgetc(fp) != 0) /* skip block trailer */
  193.            break;
  194.      }
  195.       }
  196.       fclose(fp);
  197.       return(0);
  198.    }
  199.  
  200.    strcpy(info->info_id, "GIFFILE");
  201.    info->iterations = 150;
  202.    info->fractal_type = PLASMA;
  203.    info->xmin = -1;
  204.    info->xmax = 1;
  205.    info->ymin = -1;
  206.    info->ymax = 1;
  207.    info->x3rd = -1;
  208.    info->y3rd = -1;
  209.    info->creal = 0;
  210.    info->cimag = 0;
  211.    info->videomodeax=255;
  212.    info->videomodebx=255;
  213.    info->videomodecx=255;
  214.    info->videomodedx=255;
  215.    info->dotmode = 0;
  216.    info->xdots = rows;
  217.    info->ydots = cols;
  218.    info->colors = colors;
  219.    info->version = 0;
  220.  
  221.    /* zero means we won */
  222.    fclose(fp);
  223.    return(0);
  224. }
  225.  
  226. readconfig()        /* search for, read, decode fractint.cfg file */
  227. {
  228. char tempstring[101];
  229. FILE *cfgfile;
  230. int count, i, j, ax, bx, cx, dx, dotmode, xdots, ydots, colors, commas[10];
  231. #ifdef __TURBOC__
  232. strcpy(tempstring,searchpath("fractint.cfg"));
  233. #else
  234. _searchenv("fractint.cfg","PATH",tempstring);
  235. #endif
  236. if (tempstring[0] == 0)
  237.     return(-1);                /* can't find the file  */
  238. if (strcmp(&tempstring[2],"\\\\fractint.cfg") == 0)     /* stupid answer! */
  239.     strcpy(&tempstring[2],"\\fractint.cfg");
  240. if ((cfgfile = fopen(tempstring,"r")) == NULL)
  241.     return(-1);                /* ?? can't open file   */
  242.  
  243. count = 0;                    /* build a new videomode file */
  244. while (feof(cfgfile) == 0 && count < MAXVIDEOMODES) {    /* scan through strings */
  245.     if (!fgets(tempstring, 100, cfgfile)) break;
  246.     tempstring[strlen(tempstring)-1] = 0;
  247.     if (tempstring[0] <= 32) continue;    /* comment line     */
  248.     j = 9;
  249.     for (i = 0; i <= j; i++) commas[i] = 0;
  250.     for (i = strlen(tempstring); i >= 0 && j >= 0; i--)  /* check for commas */
  251.         if (tempstring[i] == ',') {
  252.             tempstring[i] = 0;
  253.             commas[--j] = i+1;
  254.             }
  255.     sscanf(&tempstring[commas[0]],"%x",&ax);
  256.     sscanf(&tempstring[commas[1]],"%x",&bx);
  257.     sscanf(&tempstring[commas[2]],"%x",&cx);
  258.     sscanf(&tempstring[commas[3]],"%x",&dx);
  259.     dotmode     = atoi(&tempstring[commas[4]]);
  260.     xdots        = atoi(&tempstring[commas[5]]);
  261.     ydots        = atoi(&tempstring[commas[6]]);
  262.     colors        = atoi(&tempstring[commas[7]]);
  263.     if (    i >= 0 || j != 0 ||
  264.         dotmode < 0 || dotmode > 30 ||
  265.         xdots < 160 || xdots > 2048 ||
  266.         ydots < 160 || ydots > 2048 ||
  267.         (colors != 2 && colors != 4 && colors != 16 && colors != 256)
  268.         ) {
  269.         buzzer(2);
  270.         printf("\n\n There is a bad entry in fractint.cfg\n\n");
  271.         printf(" ==> %s \n", tempstring);
  272.         exit(-1);
  273.         }
  274.     tempstring[commas[8]+25] = 0;
  275.     if (commas[0] >= 25) tempstring[25] = 0;
  276.     strcpy(videoentry.name,    tempstring);
  277.     strcpy(videoentry.comment, &tempstring[commas[8]]);
  278.     if (tempstring[commas[8]] == ' ')
  279.         strcpy(videoentry.comment, &tempstring[commas[8]+1]);
  280.     videoentry.videomodeax =   ax;
  281.     videoentry.videomodebx =   bx;
  282.     videoentry.videomodecx =   cx;
  283.     videoentry.videomodedx =   dx;
  284.     videoentry.dotmode     =   dotmode;
  285.     videoentry.xdots       =   xdots;
  286.     videoentry.ydots       =   ydots;
  287.     videoentry.colors      =   colors;
  288.     tovideotable(count);
  289.     count++;
  290.     }
  291.  
  292. if (count > 0) strcpy(videoentry.name, "END");
  293. videoentry.colors = 0;
  294. tovideotable(count);
  295. fclose(cfgfile);
  296. if (count <= 0) return(-1);
  297. return(0);                    /* successful return    */
  298. }
  299.  
  300.  
  301. makeconfig()        /* routine to build a FRACTINT.CFG file */
  302. {
  303. char tempstring[101];
  304. FILE *cfgfile;
  305. int count;
  306.  
  307. #ifdef __TURBOC__
  308. strcpy(tempstring,searchpath("fractint.cfg"));
  309. #else
  310. _searchenv("fractint.cfg","PATH",tempstring);
  311. #endif
  312.  
  313. if (tempstring[0] != 0) {
  314.     buzzer(2);
  315.     printf("\n There is a FRACTINT.CFG file already located in the PATH.\n\n");
  316.     printf(" I won't make another one until after you manually remove\n");
  317.     printf(" the old one.  Safety first!\n\n");
  318.     exit(-1);
  319.     }
  320.  
  321. if ((cfgfile = fopen("fractint.cfg","w")) == NULL)
  322.     exit(-1);                /* ?? can't open file   */
  323. fprintf(cfgfile,"   Full FRACTINT.CFG File, built by a 'fractint batch=config' command\n\n");
  324. fprintf(cfgfile," name of adapter/mode    | AX | BX | CX | DX |mode|  x |  y |clrs| comments\n");
  325. fprintf(cfgfile," =============================================================================\n\n");
  326. for (count = 0; count < maxvideomode; count++) {    /* write the entries */
  327.     fromvideotable(count);
  328. #ifdef __TURBOC__
  329.     fprintf(cfgfile,"%-25.25s,%4x,%4x,%4x,%4x,%4d,%4d,%4d,%4d, %-25.25s\n",
  330. #else
  331.     fprintf(cfgfile,"%-25s,%4x,%4x,%4x,%4x,%4d,%4d,%4d,%4d, %-25s\n",
  332. #endif
  333.         videoentry.name,
  334.         videoentry.videomodeax,
  335.         videoentry.videomodebx,
  336.         videoentry.videomodecx,
  337.         videoentry.videomodedx,
  338.         videoentry.dotmode,
  339.         videoentry.xdots,
  340.         videoentry.ydots,
  341.         videoentry.colors,
  342.         videoentry.comment);
  343.     }
  344. fclose(cfgfile);
  345. exit(0);
  346. }
  347.  
  348. /* A TRULY persistent routine doing it's DARNDEST to find a good mode */
  349.  
  350. getGIFmode(v1)
  351. struct fractal_info *v1;
  352. {
  353.    int i;
  354.  
  355.    /* try EXACT match with a configured mode */
  356.     for(i=0;i<maxvideomode;i++) {
  357.        fromvideotable(i);
  358.         if(v1->videomodeax == videoentry.videomodeax &&
  359.        v1->videomodebx == videoentry.videomodebx &&
  360.        v1->videomodecx == videoentry.videomodecx &&
  361.        v1->videomodedx == videoentry.videomodedx &&
  362.        v1->dotmode       == videoentry.dotmode     &&
  363.        v1->xdots       == videoentry.xdots         &&
  364.        v1->ydots       == videoentry.ydots         &&
  365.        v1->colors       == videoentry.colors      )
  366.         break;
  367.     }
  368.     if(i<maxvideomode) /* gotit! */
  369.        return(i);
  370.  
  371.    /* try to match xdots, ydots, and colors to a configured mode */
  372.     for(i=0;i<maxvideomode;i++) {
  373.        fromvideotable(i);
  374.         if(v1->xdots       == videoentry.xdots     &&
  375.        v1->ydots       == videoentry.ydots         &&
  376.        v1->colors       == videoentry.colors      )
  377.         break;
  378.     }
  379.     if(i<maxvideomode) /* gotit! */
  380.        return(i);
  381.  
  382.     /* ABSOLUTELY the LAST gasp! ANY mode with right xdot and ydot??? */
  383.     for(i=0;i<maxvideomode;i++) {
  384.        fromvideotable(i);
  385.         if(v1->xdots       == videoentry.xdots     &&
  386.        v1->ydots       == videoentry.ydots         )
  387.         break;
  388.     }
  389.     if(i<maxvideomode) /* gotit! */
  390.        return(i);
  391.  
  392.     /* give up! */
  393.        return(maxvideomode);
  394. }
  395.